package com.it.gmall.mq.controller;

import com.it.gmall.common.result.Result;
import com.it.gmall.mq.config.DeadLetterMqConfig;
import com.it.gmall.mq.config.DelayedMsgConfig;
import com.it.gmall.mq.service.SendMsgService;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("api/mq")
public class SendMsgController {

    @Autowired
    private SendMsgService sendMsgService;

    /*
    * 生产者:rabbitTemplate
    *
    * 交换机: 无论成功和失败都响应
    * 队列: 只有失败才响应
    * 消费者: 手动应答
    *
    * 交换机和队列失败都会找生产者--调用生产者的接口*/
    @Autowired
    private RabbitTemplate rabbitTemplate;

    /*最简单版发消息 存在问题*/
    /*@GetMapping("/sendMsg/{msg}")
    private Result sendMsg(@PathVariable String msg){
        // 发送消息 (交换机 routingKey 消息实体)
        rabbitTemplate.convertAndSend("exchange111","routingKey111",msg);
        return Result.ok();
    }*/

    /*发送消息*/
    @GetMapping("/sendMsg/{msg}")
    private Result sendMsg(@PathVariable String msg){
        sendMsgService.sendMsg("exchange111","routingKey111",msg);
        return Result.ok();
    }

    /*发送基于死信的延迟消息*/
    @GetMapping("/sendMsgDeadLetter/{msg}/{ttl}")
    private Result sendMsgDeadLetter(@PathVariable String msg,@PathVariable int ttl){
        sendMsgService.sendMsgDeadLetter(DeadLetterMqConfig.exchange_dead,DeadLetterMqConfig.routing_dead_1,msg,ttl);
        return Result.ok();
    }

    /*发送基于插件的延迟消息*/
    @GetMapping("/sendMsgDelayedMsg/{msg}/{delayTime}")
    private Result sendMsgDelayedMsg(@PathVariable String msg,@PathVariable int delayTime){
        sendMsgService.sendMsgDelayedMsg(DelayedMsgConfig.exchange_delay,DelayedMsgConfig.routing_delay,msg,delayTime);
        return Result.ok();
    }
}
